﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Pumptester
{
    public partial class MainForm : Form
    {
        ExperimentForms.Base.BaseMainForm experimentForm = null;
        public MainForm()
        {
            InitializeComponent();
        }

        private void ToolStripMenuItemNew_Click(object sender, EventArgs e)
        {
            if (this.experimentForm != null)
            {
                MessageBox.Show("当前已有试验正在运行，无法同时运行多个试验!");
                return;
            }
            ChooseForm form = new ChooseForm();

            if (DialogResult.OK == form.ShowDialog())
            {
                if (form.ExperimentType == 1 && form.PumpType == 1)
                {
                    ExperimentForms.CentrifugalPump.Perf.CreateForm createForm = new ExperimentForms.CentrifugalPump.Perf.CreateForm();

                    if (createForm.ShowDialog() == DialogResult.OK)
                    {
                     
                        ExperimentForms.CentrifugalPump.Perf.MainFormEnhance mainForm = new ExperimentForms.CentrifugalPump.Perf.MainFormEnhance(createForm.Experiment);
         
                        mainForm.MdiParent = this;
                        mainForm.WindowState = FormWindowState.Maximized;
                        mainForm.ControlBox = false;
                        mainForm.ShowIcon = false;


                        this.experimentForm = mainForm;
                        experimentForm.Show();
                        experimentForm.FormClosed += ChildForm_FormClosed;
                    }
                }
                else if (form.ExperimentType == 1 && form.PumpType == 4)
                {
                    ExperimentForms.ScrewPump.Perf.CreateForm createForm = new ExperimentForms.ScrewPump.Perf.CreateForm();
                    if (createForm.ShowDialog() == DialogResult.OK)
                    {

                        ExperimentForms.ScrewPump.Perf.MainFormEnhance mainForm = new ExperimentForms.ScrewPump.Perf.MainFormEnhance(createForm.Experiment);

                        mainForm.MdiParent = this;
                        mainForm.WindowState = FormWindowState.Maximized;
                        mainForm.ControlBox = false;
                        mainForm.ShowIcon = false;

                        this.experimentForm = mainForm;
                        experimentForm.Show();
                        experimentForm.FormClosed += ChildForm_FormClosed;
                    }
                }
            }

            this.ToolStripMenuItemFinish.Enabled = this.experimentForm != null;
            this.ToolStripMenuItemNew.Enabled = this.experimentForm == null;
        }

        private void ChildForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.experimentForm = null;
            this.ToolStripMenuItemFinish.Enabled = this.experimentForm != null;
            this.ToolStripMenuItemNew.Enabled = this.experimentForm == null;
        }

        private void ToolStripMenuItemReportSearch_Click(object sender, EventArgs e)
        {
            ExperimentListForm form = new ExperimentListForm();
            form.ShowDialog();
        }

        private void ToolStripMenuItemFinish_Click(object sender, EventArgs e)
        {
            if (this.experimentForm.FinishExperiment())
            {
                
                this.ToolStripMenuItemFinish.Enabled = this.experimentForm == null;
                this.ToolStripMenuItemNew.Enabled = this.experimentForm != null;
            }
          
        }

        private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否退出系统", "退出系统", MessageBoxButtons.OKCancel))
            {
                if ((this.experimentForm != null && this.experimentForm.FinishExperiment())||(this.experimentForm == null))
                {
                    Application.Exit();
                }

            }

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
           
        }
    }
}

